package com.yanqu.road.entity.activity.commonteam.data;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.FieldTypeAnnotation;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@FieldTypeAnnotation(tableName = "t_c_common_team", desc = "通用组队-队伍表")
public class CommonTeamData extends DataObject {

    /**
     * 活动ID
     */
    @FieldTypeAnnotation(select = true, update = true, isIndex = true)
    private int activityId;

    /**
     * 分组ID
     */
    @FieldTypeAnnotation(desc = "分组ID")
    private int groupId;

    /**
     * 区服ID
     */
    @FieldTypeAnnotation(desc = "区服ID")
    private long serverId;

    /**
     * 队伍ID
     */
    @FieldTypeAnnotation(desc = "队伍ID")
    private int teamId;

    /**
     * 队伍名称
     */
    @FieldTypeAnnotation(desc = "队伍名称")
    private String teamName;

    /**
     * 队长ID
     */
    @FieldTypeAnnotation(desc = "队长ID")
    private long leaderId;

    /**
     * 队长名称
     */
    @FieldTypeAnnotation(desc = "队长名称")
    private String leaderName;

    /**
     * 总实力
     */
    @FieldTypeAnnotation(desc = "总实力")
    private long score;

    /**
     * 榜单分数MAP,k:conditionType,v:分数
     */
    @FieldTypeAnnotation(desc = "榜单分数MAP")
    private Map<Integer, CommonTeamRankScoreData> rankScoreMap = new ConcurrentHashMap<>();

    /**
     * 是否删除0未删除1已删除
     */
    @FieldTypeAnnotation(desc = "是否删除0未删除1已删除")
    private int delFlag;

    /**
     * 创建时间
     */
    @FieldTypeAnnotation(desc = "创建时间")
    private long createTime;

    public CommonTeamData() {}

    public CommonTeamData(CommonTeamData commonTeamData) {
        this.activityId = commonTeamData.getActivityId();
        this.groupId = commonTeamData.getGroupId();
        this.serverId = commonTeamData.getServerId();;
        this.teamId = commonTeamData.getTeamId();
        this.teamName = commonTeamData.getTeamName();
        this.leaderId = commonTeamData.getLeaderId();
        this.leaderName = commonTeamData.getLeaderName();
        this.score = commonTeamData.getScore();
        this.rankScoreMap = commonTeamData.getRankScoreMap();
        this.delFlag = commonTeamData.getDelFlag();
        this.createTime = commonTeamData.getCreateTime();
    }

    public CommonTeamData(int activityId, int groupId, long serverId, int teamId, String teamName, long leaderId, String leaderName, long score) {
        this.activityId = activityId;
        this.groupId = groupId;
        this.serverId = serverId;
        this.teamId = teamId;
        this.teamName = teamName;
        this.leaderId = leaderId;
        this.leaderName = leaderName;
        this.score = score;
        this.delFlag = 0;
        this.createTime = System.currentTimeMillis();
        setInsertOption();
    }

    public void setActivityId(int activityId) {
        if (this.activityId != activityId) {
            this.activityId = activityId;
            setOp(DataStatus.Update);
        }
    }

    public int getActivityId() {
        return activityId;
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        if (this.groupId != groupId) {
            this.groupId = groupId;
            setOp(DataStatus.Update);
        }
    }

    public void setServerId(long serverId) {
        if (this.serverId != serverId) {
            this.serverId = serverId;
            setOp(DataStatus.Update);
        }
    }

    public long getServerId() {
        return serverId;
    }

    public void setTeamId(int teamId) {
        if (this.teamId != teamId) {
            this.teamId = teamId;
            setOp(DataStatus.Update);
        }
    }

    public int getTeamId() {
        return teamId;
    }

    public void setTeamName(String teamName) {
        if (!Objects.equals(this.teamName, teamName)) {
            this.teamName = teamName;
            setOp(DataStatus.Update);
        }
    }

    public String getTeamName() {
        return teamName;
    }

    public void setLeaderId(long leaderId) {
        if (this.leaderId != leaderId) {
            this.leaderId = leaderId;
            setOp(DataStatus.Update);
        }
    }

    public long getLeaderId() {
        return leaderId;
    }

    public void setLeaderName(String leaderName) {
        if (!Objects.equals(this.leaderName, leaderName)) {
            this.leaderName = leaderName;
            setOp(DataStatus.Update);
        }
    }

    public String getLeaderName() {
        return leaderName;
    }

    public void setScore(long score) {
        if (this.score != score) {
            this.score = score;
            setOp(DataStatus.Update);
        }
    }

    public long getScore() {
        return score;
    }

    public Map<Integer, CommonTeamRankScoreData> getRankScoreMap() {
        return rankScoreMap;
    }

    public long getRankScore(int conditionType) {
        CommonTeamRankScoreData rankScore = rankScoreMap.get(conditionType);
        if (rankScore != null) {
            return rankScore.getScore();
        }
        return 0L;
    }

    public long getRankScoreParam(int conditionType) {
        CommonTeamRankScoreData rankScore = rankScoreMap.get(conditionType);
        if (rankScore != null) {
            return rankScore.getParam();
        }
        return 0L;
    }

    public void setRankScoreMap(Map<Integer, CommonTeamRankScoreData> rankScoreMap) {
        this.rankScoreMap = rankScoreMap;
    }

    public void putRankScore(int conditionType, long rankScore, long param) {
        CommonTeamRankScoreData info = rankScoreMap.computeIfAbsent(conditionType, k -> new CommonTeamRankScoreData());
        info.setScore(rankScore);
        info.setParam(param);
        setOp(DataStatus.Update);
    }

    public void setDelFlag(int delFlag) {
        if (this.delFlag != delFlag) {
            this.delFlag = delFlag;
            setOp(DataStatus.Update);
        }
    }

    public int getDelFlag() {
        return delFlag;
    }

    public void setCreateTime(long createTime) {
        if (this.createTime != createTime) {
            this.createTime = createTime;
            setOp(DataStatus.Update);
        }
    }

    public long getCreateTime() {
        return createTime;
    }
}
